﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Hotel.WPF.HotelWCFSurIIS;
using System.Windows.Threading;
using System.Threading;
using System.ComponentModel;

namespace Hotel.WPF.ui.Facturation
{
    /// <summary>
    /// Interaction logic for P_Facture.xaml
    /// </summary>
    public partial class P_Facture : Page
    {
        Clients monClient;
        HotelClient srvService;

        public P_Facture(Clients monClient):this()
        {
            this.monClient = monClient;
        }

        public P_Facture()
        {
            InitializeComponent();
        }

        private void Page_Loaded(object sender, RoutedEventArgs e)
        {
            try
            {
                //dg_TypeItemFacture
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void gererListeItemFacture(object sender, RoutedEventArgs e)
        {

        }

        private void bt_ajouter_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                Item_Facture itemF = new Item_Facture();

                //ch.Nom_Chzine_Hotel = tb_NomChaineHotel.Text.Trim();
                srvService = new HotelClient();
                srvService.Open();
                int resultatAjout = srvService.AddItemFacture(itemF);

                if (resultatAjout > 0)
                {
                    MessageBox.Show("L'element est bien ajoutée",
                                    "Facturation",
                                    MessageBoxButton.OK,
                                    MessageBoxImage.Information);
                    loadListeItemFacture();
                }
                else
                {
                    MessageBox.Show("Erreur d'ajout",
                                    "Facturation",
                                    MessageBoxButton.OK,
                                    MessageBoxImage.Error);
                }

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Facturation", MessageBoxButton.OK, MessageBoxImage.Error);
            }
        }

        private void loadListeItemFacture()
        {
            try
            {
                Dispatcher.BeginInvoke(DispatcherPriority.Normal,
                    (SendOrPostCallback)delegate { busyTool.IsBusy = true; },
                    null
                    );
                var backgroundWorker = new BackgroundWorker { WorkerReportsProgress = true, WorkerSupportsCancellation = true };
                backgroundWorker.DoWork += (s, args) =>
                {
                    writeToBusyTool("Chargement des elements de la facture en cours...");
                    srvService = new HotelClient();
                    srvService.Open();
                    Dispatcher.BeginInvoke(DispatcherPriority.Normal,
                                    (SendOrPostCallback)delegate
                                    {
                                        dg_ItemFacture.DataContext = srvService.getListChaineHotels();
                                    },
                                    null
                                    );


                };

                backgroundWorker.ProgressChanged += (s, args) => writeToBusyTool("");
                backgroundWorker.RunWorkerCompleted += (s, args) => Dispatcher.BeginInvoke(DispatcherPriority.Normal,
                                                                                    (SendOrPostCallback)
                                                                                    delegate
                                                                                    {
                                                                                        busyTool.IsBusy = false;
                                                                                        //srvService.Close();
                                                                                    },
                                                                                    null);
                backgroundWorker.RunWorkerAsync();

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Facturation", MessageBoxButton.OK, MessageBoxImage.Error);
            }
        }

        private void writeToBusyTool(string message)
        {
            Dispatcher.BeginInvoke(
                DispatcherPriority.Normal,
                (SendOrPostCallback)delegate
                {
                    busyTool.BusyContent = message;
                },
                null
                );
        }
    }
}
